library Integer

    struct Integer
        integer m_negative
        integer m_value
        integer m_length
        integer m_arrLength
        //单个数组元素含有数字的个数
        integer m_arrElementSize

        private integer array m_ints[16]

        public method operator [] takes integer n returns integer
            return this.m_ints[n]
        endmethod

        public method operator []= takes integer n, integer inte returns nothing
            set this.m_ints[n] = inte
            set this.m_arrLength = this.m_arrLength + 1
        endmethod

        public static method getLength takes integer i returns integer
            local integer len = 0
            if i==0 then
                return 1
            endif

            set len = 0
            loop
                exitwhen i == 0
                set i = i / 10
                set len = len + 1
            endloop

            return len
        endmethod

        static method create takes integer value returns Integer
            local Integer this = Integer.allocate()
            //debug BJDebugMsg("create Integer: " +I2S(this));
            set this.m_value = value
            set this.m_length = thistype.getLength(value)
            if value < 0 then
                set this.m_negative = -1
            else
                set this.m_negative = 1
            endif

            set m_arrLength = 0
            set m_arrElementSize = 0
            return this
        endmethod

        method destroy takes nothing returns nothing
            local integer i
            set i = 0
            loop
                exitwhen i >= this.m_length
                set this[i] = 0
                set i = i + 1
            endloop
            set this.m_negative = 1
            set this.m_value = 0
            set this.m_length = 0
            set this.m_arrLength = 0
            call this.deallocate()
        endmethod

        //size 为 1 时 ：34567 -> [7, 6, 5, 4, 3]
        //size 为 2 时 ：34567 -> [67, 45, 3]
        method toArray takes integer size returns nothing
            local integer temp = this.m_value
            local integer temp2 = 0
            local integer decimal = 0
            local integer i
            if size <= 0 then
                return
            endif
            if temp == 0 then
                set this[0] = 0
                return
            endif
            set this.m_arrElementSize = size
            set decimal = R2I(Pow(10, size))
            set i = 0
            loop
                exitwhen temp == 0
                set temp2 = temp
                set temp = temp / decimal
                set this[i] = temp2 - temp * decimal
                set i = i + 1
            endloop
        endmethod

        //整数转字符串 Integer.toFixedWidthString(56, 3) --> "056"
        static method toFixedWidthString takes integer value, integer len returns string
            local integer size = len - getLength(value)
            local integer i
            local string str = ""
            if size > 0 then
                set i = 0
                loop
                    exitwhen i >= size
                    set str = str + "0"
                    set i = i + 1
                endloop
                set str = str + I2S(value)
            else
                set str = I2S(value)
            endif
            return str
        endmethod

        method toString takes integer len returns string
            return toFixedWidthString(this.m_value, len)
        endmethod

        // size 为 1 时 ：34567 -> 7, 6, 5, 4, 3
        // size 为 2 时 ：34567 -> 67, 45, 3
        method toArrayString takes integer size returns string
            local string str = ""
            local integer i
            call this.toArray(size)
            set i = 0
            loop
                exitwhen i >= this.m_arrLength
                if i == this.m_arrLength - 1 then
                    set str = str + (I2S(this[i]))
                else
                    set str = str + (I2S(this[i]) + ",")
                endif
                set i = i + 1
            endloop
            return str
        endmethod
    endstruct

endlibrary